package yl.hs.bmipfull.service.laboratorydata.imple;


import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.laboratorydata.ILabRmFlyashInspectionItemDao;
import yl.hs.bmipfull.daos.laboratorydata.ILabRmInspectionRecordDao;
import yl.hs.bmipfull.pojo.laboratorydata.LabRmCementReportItemEntity;
import yl.hs.bmipfull.pojo.laboratorydata.LabRmFlyashInspectionItemEntity;
import yl.hs.bmipfull.pojo.laboratorydata.LabRmFlyashReportItemEntity;
import yl.hs.bmipfull.pojo.laboratorydata.LabRmInspectionRecordEntity;
import yl.hs.bmipfull.service.laboratorydata.ILabRmFlyashInspectionItemService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.List;
import java.util.Map;

/**
 * @author 吴高丰
 * @apiNote 实验记录-粉煤灰实验值服务层接口实现
 * @since 2022年06月30日 15时34分01秒 <br/>
 */
@Service
public class LabRmFlyashInspectionItemServiceImpl extends LaboratorydataBaseService implements ILabRmFlyashInspectionItemService {

    @Autowired
    ILabRmFlyashInspectionItemDao dao;
    @Autowired
    ILabRmInspectionRecordDao recordDao;

    @Override
    public List<LabRmFlyashInspectionItemEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(LabRmFlyashInspectionItemEntity.class);
        pageQuery.getQuery().put("sortKey", String.join(",", pageQuery.getSortSides()));
        pageQuery.getQuery().put("unitSNArray", getCompanySNList());
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(), LabRmFlyashInspectionItemEntity.class);
        return dao.queryList(pageQuery.getQuery(), filters);
    }

    @Override
    public PageResult<LabRmFlyashInspectionItemEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(LabRmFlyashInspectionItemEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<LabRmFlyashInspectionItemEntity> result = new PageResult(rowNum);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(),LabRmFlyashInspectionItemEntity.class);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery(),filters));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery(),filters));
        return result;
    }


    @Override
    public LabRmFlyashInspectionItemEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveLabRmFlyashInspectionItem(Map<String, Object> model, String sn) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        Integer result = 0;
        LabRmInspectionRecordEntity recordEntity=null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            recordEntity = new LabRmInspectionRecordEntity();
            Tl.getChangeLog(recordEntity, model);
            recordEntity.setSn(queryNewSN(LabRmInspectionRecordEntity.class));
            recordEntity.setPYFieldValue();
            result = recordDao.insertNew(recordEntity);
            if (result > 0) {
                writeLog(ActionType.CREATE, recordEntity.getSn(), "插入实验记录-粉煤灰实验值信息", Tl.toJson(recordEntity), LabRmInspectionRecordEntity.class);
            }
            var subData=model.get("subData");
            var subDataEntity=objectMapper.convertValue(subData, LabRmFlyashInspectionItemEntity.class);
            subDataEntity.setInspectionSN(recordEntity.getSn());
            result = dao.insertNew(subDataEntity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新实验记录-粉煤灰实验值信息", Tl.toJson(subDataEntity), LabRmFlyashInspectionItemEntity.class);
            }

        } else {
            //执行修改
            recordEntity = recordDao.findBySN(sn);
            Tl.getChangeLog(recordEntity, model);
            recordEntity.setPYFieldValue();
            result = recordDao.update(recordEntity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新实验记录-粉煤灰实验值信息", Tl.toJson(recordEntity), LabRmInspectionRecordEntity.class);
            }
            dao.deleteBySn(recordEntity.getSn());
            var subData=model.get("subData");
            var subDataEntity=objectMapper.convertValue(subData, LabRmFlyashInspectionItemEntity.class);
            subDataEntity.setInspectionSN(recordEntity.getSn());
            result = dao.insertNew(subDataEntity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新实验记录-粉煤灰实验值信息", Tl.toJson(subDataEntity), LabRmFlyashInspectionItemEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String  sn) {
        Integer result = dao.deleteBySn(sn);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, LabRmFlyashInspectionItemEntity.class);
        }
        return result;
    }

    @Override
    public List<LabRmFlyashInspectionItemEntity> getByInspectionSN(String inspectionSN) {
        return dao.findByInspectionSN(inspectionSN);
    }
}